package com.ckk.hystrix.service;

import com.ckk.hystrix.interfaces.ServiceFeignByCheck;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class CheckService {
    @Resource
    private ServiceFeignByCheck serviceFeignByCheck;

    // 通过feign调用服务模块，如果触发熔断机制，则会调用此注解fallbackMethod定义的方法
    @HystrixCommand(fallbackMethod = "checkError")
    public Map check(String username) {
        return serviceFeignByCheck.selectClient(username);
    }

    public Map checkError(String username) {
        Map map = new HashMap();
        map.put("code", 500);
        map.put("msg","服务异常，请稍后重试！");
        map.put("username", username);
        return map;
    }
}
